/*
 Copyright (C) 2009 are of boxtorrents
 boxtorrents, azureus, vuze, and java are copyrighted to their respective owner.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
package com.boxtorrents.azureus.action;

import org.gudy.azureus2.plugins.logging.LoggerChannel;

import com.boxtorrents.azureus.PluginParamConstants;
import com.boxtorrents.azureus.StatCheckListener;
import com.boxtorrents.azureus.StatCheckEvent;
import com.boxtorrents.azureus.BoxTorrentsPlugin;

/**
 * Notify user about check result.
 * 
 * @author are of boxtorrents
 */
public class NotificationAction implements StatCheckListener {
	private static final long serialVersionUID = 4558770185427170049L;
	private int last = -1;

	private void onDangerCheck(StatCheckEvent e) {
		LoggerChannel log = BoxTorrentsPlugin.getLog();
		if (isAlwaysShow() || last != e.getEventType()) {
			log.logAlert(LoggerChannel.LT_INFORMATION, BoxTorrentsPlugin
			    .getLocaleUtil().localise("message.action.notify.alert.danger")
			    + e.toString());
		} else {
			log.log(e.toString());
		}
	}

	private void onNormalCheck(StatCheckEvent e) {
		LoggerChannel log = BoxTorrentsPlugin.getLog();
		if (isAlwaysShow() || last != e.getEventType()) {
			log.logAlert(LoggerChannel.LT_INFORMATION, BoxTorrentsPlugin
			    .getLocaleUtil().localise("message.action.notify.alert.normal")
			    + e.toString());
		} else {
			log.log(e.toString());
		}
	}

	private void onWarningCheck(StatCheckEvent e) {
		LoggerChannel log = BoxTorrentsPlugin.getLog();
		if (isAlwaysShow() || last != e.getEventType()) {
			log.logAlert(LoggerChannel.LT_INFORMATION, BoxTorrentsPlugin
			    .getLocaleUtil().localise("message.action.notify.alert.warning")
			    + e.toString());
		} else {
			log.log(e.toString());
		}
	}

	/**
	 * @inheritDoc
	 */
	public boolean isEnabled() {
		return BoxTorrentsPlugin.getPluginInterface().getPluginconfig()
		    .getPluginBooleanParameter(PluginParamConstants.PR_NOTIFY_ENABLE, true);
	}

	private boolean isAlwaysShow() {
		return BoxTorrentsPlugin
		    .getPluginInterface()
		    .getPluginconfig()
		    .getPluginBooleanParameter(PluginParamConstants.PR_NOTIFY_ALWAYS, false);
	}

	/**
	 * @inheritDoc
	 */
	public void onCheckResult(StatCheckEvent e) {
		if (e.getEventType() == StatCheckEvent.EVENT_DANGER) {
			onDangerCheck(e);
		} else if (e.getEventType() == StatCheckEvent.EVENT_WARNING) {
			onWarningCheck(e);
		} else if (e.getEventType() == StatCheckEvent.EVENT_NORMAL) {
			onNormalCheck(e);
		}
		last = e.getEventType();
	}

	/**
	 * @inheritDoc
	 */
	public void onEnabled(boolean enabled) {
		if (!enabled) last = -1;
	}
}
